package com.younion.business.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.younion.business.IAdvertisingService;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.common.util.ImageFileUtil;
import com.younion.dao.AdvertisingDAO;
import com.younion.vo.API_AdvertisingVO;
import com.younion.vo.AdvertisingVO;
import com.younion.vo.FavourableEntityVO;

import cpcns.jhsks.util.MapCollectionUtils;
 
public class AdvertisingServiceImpl extends BaseService implements IAdvertisingService {
	
protected static final Log logger = LogFactory.getLog(AdvertisingServiceImpl.class);
	
	private AdvertisingDAO advertisingDAO;
	
	public List get(Map paramMap, Paginator paginator) throws BaseException {
		if (MapCollectionUtils.isEmpty(paramMap))
			return null;
		try {
			Map map = new HashMap();
			map.put("paramMap", paramMap);
			return this.advertisingDAO.selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					AdvertisingServiceImpl.class.getName()
							+ " >> public Page get(Map paramMap, Integer curPage, Integer pageSize) error occur:",
					e);
		}
	}

	public int getCountByMap(Map paramMap) throws BaseException {
		try {
			return this.advertisingDAO.selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public Collection getCountByMap(Map map) error occur:", e);
		}
	}

	public Integer save(AdvertisingVO voObject) throws BaseException {
		try {
			return this.advertisingDAO.insertByVo(voObject);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public AdvertisingVO save(AdvertisingVO voObject) error occur:", e);
		}
	}
	
	public boolean deleteAdvertisingById(Map paramMap) throws BaseException {
		try {
			return this.advertisingDAO.deleteAdvertisingById(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public boolean deleteAdvertisingById(Map paramMap) error occur:", e);
		}
	}

	public boolean deleteAdvertisingByIds(List list) throws BaseException {
		try {
			return this.advertisingDAO.deleteAdvertisingByIds(list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public boolean deleteAdvertisingByIds(List list) error occur:", e);
		}
	}

	public AdvertisingVO selectAdvertisingById(Map paramMap) throws BaseException {
		try {
			return this.advertisingDAO.selectAdvertisingById(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public AdvertisingVO selectAdvertisingById(Map paramMap) error occur:", e);
		}
	}

	public boolean updateAdvertisingByMap(Map paramMap) throws BaseException {
		try {
			return this.advertisingDAO.updateAdvertisingByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public boolean updateAdvertisingByMap(Map paramMap) error occur:", e);
		}
	}
	
	public int selectMaxTop() throws BaseException {
		try {
			return this.advertisingDAO.selectMaxTop();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public int selectMaxTop() error occur:", e);
		}
	}
	
	public List<AdvertisingVO> selectByIds(List list) throws BaseException {
		try {
			return this.advertisingDAO.selectByIds(list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public List<AdvertisingVO> selectByIds(List list) error occur:", e);
		}
	}

	public void updateStateByIds(Map map) throws BaseException {
		try {
			this.advertisingDAO.updateStateByIds(map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public void updateStateByIds(Map map) error occur:", e);
		}
	}

	public void updateTopById(Map map) throws BaseException {
		try {
			this.advertisingDAO.updateTopById(map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(AdvertisingServiceImpl.class.getName()
					+ " >> public void updateTopById(Map map) error occur:", e);
		}
	}
	
	public List<API_AdvertisingVO> getApiAdvertisement(Map map) throws Exception{
		try{
			return advertisingDAO.getApiAdvertisement(map);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public getApiAdvertisement error occur:",e);
		}
	}
	
	public boolean deleteImgByCustomerIds(List<String> customerIds , String path) throws BaseException{
		if(CollectionUtils.isEmpty(customerIds)){
			return false;
		}
		try{
			Map map = new HashMap();
			for(String customerId : customerIds){
				map.put("customerId", customerId);
				List<AdvertisingVO> adList = (List<AdvertisingVO>) advertisingDAO.selectByMap(map);
				for(AdvertisingVO ad : adList){
					File directory = new File(path + File.separator
							+ ad.getId());
					String fileNames = ad.getPicNames();
					ImageFileUtil.deleteImage(directory, fileNames, true);	
				}
				map.clear();
			}
			return true;
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public Collection deleteImgByCustomerIds(List customerIds , String path) error occur:",e);
			
		}
	}
	public void setAdvertisingDAO(AdvertisingDAO advertisingDAO) {
		this.advertisingDAO = advertisingDAO;
	}
	
	public AdvertisingDAO getAdvertisingDAO() {
		return this.advertisingDAO;
	}

}
